package com.erpoa.service.impl;

import com.erpoa.annotation.DataBase;
import com.erpoa.constants.DataSourceNames;
import com.erpoa.entity.MgUser;
import com.erpoa.mapper.PdaLoginServiceMapper;
import com.erpoa.service.PdaLoginService;
import com.erpoa.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjimin
 * @Title: pdaLoginServiceImpl
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/9/114:13
 */
@Service
public class PdaLoginServiceImpl implements PdaLoginService {
    private static final Logger logger = LoggerFactory.getLogger(PdaLoginServiceImpl.class);

    @Autowired
    private PdaLoginServiceMapper pdaLoginServiceMapper;

    @Override
    @DataBase(DataSourceNames.DSX5)
    public MgUser getUserFromDataBase(String scode) {
        // 获得用户信息
        MgUser user = this.pdaLoginServiceMapper.selectUserBysCode(scode);
        if (StringUtil.isNull(user)) {
            return null;
        }
        // 获得权限信息
        List<Integer> codes = this.pdaLoginServiceMapper.selectPerssionBysCode(scode);
        if (StringUtil.isNotEmpty(codes)) {
            user.setUserPerssion(codes);
        } else {
            //防止报错，扔一个空值
            user.setUserPerssion(new ArrayList<>());
        }
        return user;
    }

    @Override
    @DataBase(DataSourceNames.DSX5)
    public String getPDAStatus(String deviceID) {
        Integer status = this.pdaLoginServiceMapper.getPDAStatus(deviceID);
        if (status > 0) {
            return "OK";
        }
        return "此PDA尚未注册,请先注册!";
    }

    @Override
    @DataBase(DataSourceNames.DSX5)
    @Async(value = "hjSimpleExecutor")
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer updateorInsertPdaVersion(String deviceid, String version, String sCode) {
        return this.pdaLoginServiceMapper.updateorInsertPdaVersion(deviceid, version, sCode);
    }
}
